package malvados.app;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import malvados.app.bin.TouchImageView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.ZoomControls;

public class ZoomView extends Activity{

	public ZoomView() {
		// TODO Auto-generated constructor stub
	}
	
	private TouchImageView imgTirinha;
	private ProgressDialog dialog;
	private ZoomControls zoomControl;
	private int IdTirinha;
	Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
	
	public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zoom);
        
        imgTirinha = (TouchImageView)findViewById(R.id.imgZoomTirinha);
        zoomControl = (ZoomControls)findViewById(R.id.zoomControl);
        savedMatrix = imgTirinha.getImageMatrix();
        zoomControl.setIsZoomOutEnabled(false);
        
        PackageManager pm = getPackageManager();
        boolean hasMultitouch = 
            pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
        if (hasMultitouch) {
        	//zoomControl.setIsZoomInEnabled(false);
        	zoomControl.hide();
        } else {
            
        }
        
        zoomControl.setOnZoomInClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				ScaleAnimation anim = new ScaleAnimation((float)1.0, (float)1.5, (float)1.0, (float)1.5);
				anim.setDuration(250);
				anim.setFillAfter(true);
				anim.setFillEnabled(true);
				imgTirinha.startAnimation(anim);
				//zoomControl.setIsZoomInEnabled(false);
				//zoomControl.setIsZoomOutEnabled(true);
			}
		});
        
        zoomControl.setOnZoomOutClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				ScaleAnimation anim = new ScaleAnimation((float)1.5, (float)1.0, (float)1.5, (float)1.0);
				anim.setDuration(250);
				anim.setFillAfter(true);
				anim.setFillEnabled(true);
				imgTirinha.startAnimation(anim);
				//zoomControl.setIsZoomInEnabled(true);
				//zoomControl.setIsZoomOutEnabled(false);
			}
		});
        
        IdTirinha = getIntent().getExtras().getInt("IdTirinha");
        carregaImagens(IdTirinha);
        
    }
	
	public void carregaImagens(int Id){
        try {
            //this.IdTirinha = Id;
            dialog = ProgressDialog.show(ZoomView.this, "","Carregando. Aguarde...", true);
            new carregaImagens().execute(this.imgTirinha);
//            if(ListaFavoritos.verificaFavorito(new Favorito(String.valueOf(IdTirinha),"")))
//            	btnFavorito.setImageDrawable(getResources().getDrawable(R.drawable.favorito) );
//            else
//            	btnFavorito.setImageDrawable(getResources().getDrawable(R.drawable.naofavorito) );
            	
            //new carregaImagensMarginais().cancel(true);
            //new carregaImagensMarginais().execute();
            
        } catch (Exception ex) {
            //Logger.getLogger(MalvadosActivity.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
	
	class carregaImagens extends AsyncTask<ImageView, Void, Drawable>{

        ImageView tirinha = null;
        @Override
        protected Drawable doInBackground(ImageView... imageView){
            this.tirinha = imageView[0];
            return getTirinha(IdTirinha);
        }

        @Override
        protected void onPostExecute(Drawable result) {
            //tirinha.setImageDrawable(getResources().getDrawable(R.drawable.tirinha));
            //topo.setImageDrawable(getResources().getDrawable(R.drawable.logo));
        	tirinha.setImageDrawable(result);
            dialog.dismiss();
        }
    }
	
	public Drawable getTirinha(int Id){
        Drawable retorno = null;
        try {
            if(MalvadosActivity.MapaTirinhas.containsKey(String.valueOf(Id))){
                retorno = MalvadosActivity.MapaTirinhas.get(String.valueOf(Id));
            }else{
                retorno = getImage("http://www.malvados.com.br/tirinha" + Id + ".jpg");
                MalvadosActivity.MapaTirinhas.put(String.valueOf(Id), retorno);
            }
            return retorno;
        } catch (Exception ex) {
            try {
                retorno = getImage("http://www.malvados.com.br/tirinha" + Id + ".gif");
                MalvadosActivity.MapaTirinhas.put(String.valueOf(Id), retorno);
                return retorno;
            } catch (Exception ex1) {
            	imgTirinha.setImageDrawable(getResources().getDrawable(R.drawable.tirinha) );
                //Logger.getLogger(MalvadosActivity.class.getName()).log(Level.SEVERE, null, ex1);
            }
            
        }
        return null;
    }
	
public Drawable getImage(String caminho)  {
        
		try {
			URL url;
			url = new URL(caminho);
			InputStream is;
			is = (InputStream) getObjeto(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        return null;    
    }

	private Object getObjeto(URL url) {
	    
		try {
			Object content;
			content = url.getContent();
			return content;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return null;
	}

}
